/*
 * @Author: Vonwell 529538187@qq.com
 * @Date: 2024-07-31 22:57:23
 * @LastEditors: Vonwell 529538187@qq.com
 * @LastEditTime: 2024-09-06 11:19:30
 * @FilePath: \ext-oled12864-master\arduinoC\main.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

enum SIZE {
    //% block="29*29"
    1,
    //% block="58*58"
    2
}

enum LINE {
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="3"
    3,
    //% block="4"
    4
}

enum BTN {
    //% block="A"
    A,
    //% block="B"
    B,
    //% block="A+B"
    AB
}


//% color="#AA278D" iconWidth=50 iconHeight=40
namespace MyOledshow {
    
    //===============================OLED屏幕==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="传感器: OLED屏幕" blockType="tag"
    export function noteSep() {}

    //% block="Init [initPinx] [initPiny]" blockType="command"
    //% initPinx.shadow="dropdown"  initPinx.options=PINX
    //% initPiny.shadow="dropdown"  initPiny.options=PINY 
    export function Init(parameter: any, block: any) {
        if(Generator.board=="firebeetleesp32e")
            {
                Generator.addInclude('Myoled.Arduino', '#include <Arduino.h>');
                Generator.addInclude('Myoled.U8g2lib', '#include <U8g2lib.h>');
                Generator.addInclude('Myoled.Wire', '#include <Wire.h>');
                Generator.addSetupMainTop("Myoled.begin", "u8g2.begin();");
                Generator.addSetupMainTop("Myoled.enableUTF8Print", "u8g2.enableUTF8Print();");                        
                Generator.addObject(`myoled`, `U8G2_SSD1306_128X64_NONAME_F_SW_I2C`, `u8g2(U8G2_R0, /* clock=*/ D2, /* data=*/ D3, /* reset=*/ U8X8_PIN_NONE);`);
            }      
    }

    //% block="setchinese" blockType="command"
    export function SetChinese(parameter: any, block: any) {
        if(Generator.board=="firebeetleesp32e")
            {
                Generator.addCode(`u8g2.setFont(u8g2_font_unifont_t_chinese2);`);
            }      
    }
    
    //% block="setchinese" blockType="command"
    export function ClearBuf(parameter: any, block: any) {
        if(Generator.board=="firebeetleesp32e")
            {
                Generator.addCode(`u8g2.clearBuffer();`);
            }      
    }
    //% block="show [INPUT]at x [X] y [Y]" blockType="command"
    //% INPUT.shadow="string" INPUT.defl=hello
    //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=0
    //% Y.shadow="range" Y.params.min=0 Y.params.max=63 Y.defl=0 
    export function Printoled(parameter: any, block: any) {
        let str = parameter.INPUT.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        if(Generator.board=="firebeetleesp32e")
            {
                
                Generator.addCode(`u8g2.setCursor(${x},${y});`);
                Generator.addCode(`u8g2.print(${str});`);
            }      
    }




//=================这里OLED显示操作相关设置

    //% block="05-设置字体方向[DICfx]" blockType="command"
    //% DICfx.shadow="dropdown"  DICfx.options=DICfxP
    export function SetChineseDIC(parameter: any, block: any) {
        let DICfxs = parameter.DICfx.code;
        if(Generator.board=="firebeetleesp32e")
            {
                //设置字体显示方向
                Generator.addCode(`u8g2.setFontDirection(${DICfxs});`);
            }      
    }

    //% block="06-绘制矩形[HZ111] X:[HZ11] Y:[HZ12] 宽:[k] 高:[g](OLED模块初始化后使用)" blockType="command"
     //% HZ111.shadow="dropdown"HZ111.options=HZ111P
    //% HZ11.shadow="range" HZ11.params.min=0 HZ11.params.max=127 HZ11.defl=0
    //% HZ12.shadow="range" HZ12.params.min=0 HZ12.params.max=63 HZ12.defl=0 
    //% k.shadow="range" k.params.min=0 k.params.max=63 k.defl=10
    //% g.shadow="range" g.params.min=0 g.params.max=63 g.defl=10
    export function HZ1(parameter: any, block: any) {
        let HZ111= parameter.HZ111.code;
        let HZ11s = parameter.HZ11.code;
        let HZ12s = parameter.HZ12.code;
        let kk = parameter.k.code;
        let gg = parameter.g.code;
        if(Generator.board=="firebeetleesp32e")
            {
                if(HZ111=="drawFrame")
                    {
                    //绘制shiti
                    Generator.addCode(`u8g2.drawFrame(${HZ11s}, ${HZ12s},${kk},${gg});`);
                    }

                if(HZ111=="drawBox")
                {
                    //绘制
                    Generator.addCode(`u8g2.drawBox(${HZ11s}, ${HZ12s},${kk},${gg});`);
                }
            }      
    }


    //% block="07-绘制像素 X:[HHZ11] Y:[HHZ12](OLED模块初始化后使用)" blockType="command"
    //% HHZ11.shadow="string" HHZ11.defl=0
    //% HHZ12.shadow="string" HHZ12.defl=0
    export function HHZ1(parameter: any, block: any) {
        let HHZ11s = parameter.HHZ11.code;
        let HHZ12s = parameter.HHZ12.code;
        if(Generator.board=="firebeetleesp32e")
            {
                //绘制像素
                Generator.addCode(`u8g2.drawPixel(${HHZ11s}, ${HHZ12s});`);
            }      
    }




    //% block="08-绘制直线 开始位置X:[x0] 开始位置Y:[y0] 结束位置X[x1] 结束位置Y[y1](OLED模块初始化后使用)" blockType="command"
    //% x0.shadow="number" x0.defl=0
    //% y0.shadow="number" y0.defl=0
    //% x1.shadow="number" x1.defl=0
    //% y1.shadow="number" y1.defl=0
    export function hzzx(parameter: any, block: any) {
        let x0s = parameter.x0.code;
        let y0s = parameter.y0.code;
        let x1s = parameter.x1.code;
        let y1s = parameter.y1.code;
        if(Generator.board=="firebeetleesp32e")
            {
                //绘制像素
                Generator.addCode(`u8g2.drawLine(${x0s}, ${y0s}, ${x1s}, ${y1s});`);
            }      
    }

//=================这里OLED画图结束





    //% block="09-OLED屏幕开始显示信息 (OLED)" blockType="command"
    export function Sendmsgshow(parameter: any, block: any) {
        if(Generator.board=="firebeetleesp32e")
            {
                Generator.addCode(`u8g2.sendBuffer();`);
            }      
    }

    //===============================DHT11温湿度传感器==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="传感器: DHT11/22" blockType="tag"
    export function noteSep1() {}
    //% block="xz [DHTIO] cgq [DHT1122] gn [DHTinfo]" blockType="reporter"
    //% DHTIO.shadow="dropdown"  DHTIO.defl=hello DHTIO.options=PINDHTT
    //% DHT1122.shadow="dropdown"   DHT1122.options=PINDHTT1122
    //% DHTinfo.shadow="dropdown"   DHTinfo.options=PINDDHTinfo
    export function GetDHT(parameter: any, block: any) {
        let dhtIO = parameter.DHTIO.code;
        let dht1122 = parameter.DHT1122.code;
        let dhtinfo = parameter.DHTinfo.code;
        if(Generator.board=="firebeetleesp32e")
            {           
                Generator.addInclude('Myoled.GetDHT', '#include <DFRobot_DHT.h>');        
                Generator.addObject(`myoled.GetDHT1`, `DFRobot_DHT`, `${dht1122}_${dhtIO};`);
                Generator.addSetup(`myoled.GetDHT1info`,`${dht1122}_${dhtIO}.begin(${dhtIO}, ${dht1122});`);
                Generator.addCode(`${dht1122}_${dhtIO}.${dhtinfo}`);
            }      
    }



    //===============================URM超声波传感器==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="传感器: URM超声波/SR04" blockType="tag"
    export function noteSep2() {}

    //% block="xz [ChaosbCM] cgq [ChaosbTrigIO] gn [ChaosbEchoIOTinfo]" blockType="reporter"
    //% ChaosbCM.shadow="dropdown"   ChaosbCM.options=PINChaosbCM
    //% ChaosbTrigIO.shadow="dropdown"   ChaosbTrigIO.options=PINChaosbTrigIO
    //% ChaosbEchoIOTinfo.shadow="dropdown"   ChaosbEchoIOTinfo.options=PINChaosbEchoIOTinfo
    export function Chaosb(parameter: any, block: any) {
        let chaosbCM = parameter.ChaosbCM.code;
        let chaosbTrigIO = parameter.ChaosbTrigIO.code;
        let chaosbEchoIOTinfo = parameter.ChaosbEchoIOTinfo.code;
        if(Generator.board=="firebeetleesp32e")
            {           
                Generator.addInclude('Myoled.Chaosb', '#include <DFRobot_URM10.h>');        
                Generator.addObject(`myoled.Chaosb1`, `DFRobot_URM10`, `urm10;`);
                //Generator.addSetup(`myoled.Chaosb1info`,`${dhtIO}_${dht1122}.begin(${dhtIO}, ${dht1122});`);
                Generator.addCode(`(urm10.${chaosbCM}(${chaosbTrigIO}, ${chaosbEchoIOTinfo}))`);
            }      
    }



    //===============================全模拟口==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="GPIO引脚操作" blockType="tag"
    export function noteSep3() {}

    //% block="Xun[mnkxz] set[MNK]" blockType="reporter"
    //% mnkxz.shadow="dropdown"   mnkxz.options=MNKP
    //% MNK.shadow="dropdown"   MNK.options=MNKP
    export function MONIKOU(parameter: any, block: any) {
        let MNKs = parameter.MNK.code;
        if(Generator.board=="firebeetleesp32e")
            {               
                Generator.addCode(`analogRead(${MNKs})`);
            }      
    }
   //===============================全数字口==========================================
    //==============================这里是提示积木====================================
    //===============================================================================

    //% block="Xun[szkxz] set[gddw]" blockType="command"
    //% szkxz.shadow="dropdown"   szkxz.options=szkxzP
    //% gddw.shadow="dropdown"   gddw.options=gddwP
    export function SZK(parameter: any, block: any) {
        let szkxzs = parameter.szkxz.code;
        let gddws = parameter.gddw.code;
        if(Generator.board=="firebeetleesp32e")
            {               
                Generator.addCode(`digitalWrite(${szkxzs},${gddws});`);
            }      
    }


   //===============================读取数字口==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="读取数字口[qdszkz]值" blockType="boolean"
    //% qdszkz.shadow="dropdown"   qdszkz.options=szkxzP
    export function qdszk(parameter: any, block: any) {
        let qdszkzs = parameter.qdszkz.code;
        if(Generator.board=="firebeetleesp32e")
            {               
                Generator.addCode(`(digitalRead(${qdszkzs}))`);
            }      
    }



   //===============================设置PWM口值==========================================
    //==============================这里是提示积木====================================
    //===============================================================================
    //% block="设置PWM[pwmyj]值为[pwmz]" blockType="command"
    //% pwmyj.shadow="dropdown"   pwmyj.options=szkxzP
    //% pwmz.shadow="range"   pwmz.params.min=0    pwmz.params.max=1024    pwmz.defl=5
    export function SZPWM(parameter: any, block: any) {
        let pwmyjS = parameter.pwmyj.code;
        let pwmzs = parameter.pwmz.code;
        if(Generator.board=="firebeetleesp32e")
            {               
                Generator.addCode(`analogWrite(${pwmyjS}, ${pwmzs});`);
            }      
    }


    function replace(str :string) {
        return str.replace("+", "");
    }
}
